// pages/technicians/technicians.js
var api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
     lists:[],
    page:1,
    lastPage:0,
    longitude:'',
    latitude:'',
    shop_id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let location = wx.getStorageSync('Location');
    if(location){
      this.data.latitude = location.latitude;
      this.data.longitude = location.longitude;
      
    }
    this.setData({
      shop_id:options.shop_id
    })
    this.getWorkers();
  },
  selectWorker(e){
    var pages = getCurrentPages(); // 获取页面栈
    var prevPage = pages[pages.length - 2]; // 上一个页面
    let worker = e.currentTarget.dataset.worker;
    prevPage.setData({
      workerId: worker.id,
      workerTxt:worker.name+" | "+worker.phone
    });
    wx.navigateBack({
      delta: 1,
    })
  },
  //店铺详情
  click_store_detail(e){
    let index = e.currentTarget.dataset.index;
    let shopId = this.data.lists[index].id;
    wx.navigateTo({
      url: '../store_detail/store_detail?shop_id='+shopId,
    })
  },
  getWorkers(){
    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
    api.get('/common/workers',{
      longitude:this.data.longitude,
      latitude:this.data.latitude,
      shop_id:this.data.shop_id
    }).then(res=>{
      this.setData({
        lists:res.shops,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
  })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getWorkers();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})